﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArtOfTest.WebAii.Controls.HtmlControls;

namespace SM2SetupSearch
{
    public class TableOfSearchActions
    {
        public static Boolean is_advanced_search_keywords_loaded()
        {
            try
            {
                Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                HtmlDiv cphPageContent_pnlSearchSetup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlSearchSetup");
                HtmlDiv cphPageContent_upKeywordsList = cphPageContent_pnlSearchSetup.Find.ById<HtmlDiv>("cphPageContent_upKeywordsList");
                HtmlTable cphPageContent_gvKeywords = cphPageContent_upKeywordsList.Find.ById<HtmlTable>("cphPageContent_gvKeywords");

                int intCountRows = cphPageContent_gvKeywords.Rows.Count;
                if (intCountRows > 0) return true;
                else return false;
            }
            catch
            {
                return false;
            }
        }

        public static HtmlInputCheckBox GetCheckBox(string strSearchWords)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                SM2Utilities.CommonLoader.load("advanced_search_keywords.aspx");
            }
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlSearchSetup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlSearchSetup");
            HtmlDiv cphPageContent_upKeywordsList = cphPageContent_pnlSearchSetup.Find.ById<HtmlDiv>("cphPageContent_upKeywordsList");
            HtmlTable cphPageContent_gvKeywords = cphPageContent_upKeywordsList.Find.ById<HtmlTable>("cphPageContent_gvKeywords");
            int intCountRows = cphPageContent_gvKeywords.Rows.Count;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvKeywords.AllRows[i];
                HtmlTableCell tblCell_SearchWords = tblrow.Cells[4];

                string strCell_SearchWords = tblCell_SearchWords.TextContent.Trim();
                if (strCell_SearchWords.Contains(strSearchWords))
                {
                    HtmlTableCell tblCell_Checkbox = tblrow.Cells[0];
                    IList<HtmlInputCheckBox> iListControls = tblCell_Checkbox.Find.AllByAttributes<HtmlInputCheckBox>();
                    HtmlInputCheckBox checkbox = iListControls[0];
                    return checkbox;
                }
            }
            return null;
        }

        public static HtmlInputCheckBox GetCheckBoxAll(string strSearchWords)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                SM2Utilities.CommonLoader.load("advanced_search_keywords.aspx");
            }
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlSearchSetup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlSearchSetup");
            HtmlDiv cphPageContent_upKeywordsList = cphPageContent_pnlSearchSetup.Find.ById<HtmlDiv>("cphPageContent_upKeywordsList");
            HtmlTable cphPageContent_gvKeywords = cphPageContent_upKeywordsList.Find.ById<HtmlTable>("cphPageContent_gvKeywords");

            HtmlTableRow tblrow = cphPageContent_gvKeywords.AllRows[0];

            HtmlTableCell tblCell_Checkbox = tblrow.Cells[0];
            IList<HtmlInputCheckBox> iListControls = tblCell_Checkbox.Find.AllByAttributes<HtmlInputCheckBox>();
            HtmlInputCheckBox checkbox = iListControls[0];
            return checkbox;
        }

        public static HtmlAnchor GetEditAnchor(string strSearchWords)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                SM2Utilities.CommonLoader.load("advanced_search_keywords.aspx");
            }
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlSearchSetup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlSearchSetup");
            HtmlDiv cphPageContent_upKeywordsList = cphPageContent_pnlSearchSetup.Find.ById<HtmlDiv>("cphPageContent_upKeywordsList");
            HtmlTable cphPageContent_gvKeywords = cphPageContent_upKeywordsList.Find.ById<HtmlTable>("cphPageContent_gvKeywords");
            int intCountRows = cphPageContent_gvKeywords.Rows.Count;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvKeywords.AllRows[i];
                HtmlTableCell tblCell_SearchWords = tblrow.Cells[4];

                string strCell_SearchWords = tblCell_SearchWords.TextContent.Trim();
                if (strCell_SearchWords.Contains(strSearchWords))
                {
                    HtmlTableCell tblCell = tblrow.Cells[2];
                    IList<HtmlAnchor> iListControls = tblCell.Find.AllByAttributes<HtmlAnchor>();
                    HtmlAnchor anchor = iListControls[0];
                    return anchor;
                }
            }
            return null;
        }

        public static string GetIDKeyword(string strSearchWords)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                SM2Utilities.CommonLoader.load("advanced_search_keywords.aspx");
            }

            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlSearchSetup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlSearchSetup");
            HtmlDiv cphPageContent_upKeywordsList = cphPageContent_pnlSearchSetup.Find.ById<HtmlDiv>("cphPageContent_upKeywordsList");
            HtmlTable cphPageContent_gvKeywords = cphPageContent_upKeywordsList.Find.ById<HtmlTable>("cphPageContent_gvKeywords");

            int intCountRows = cphPageContent_gvKeywords.Rows.Count;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvKeywords.AllRows[i];
                HtmlTableCell tblCell_SearchWords = tblrow.Cells[4];

                string strCell_SearchWords = tblCell_SearchWords.TextContent.Trim();
                if (strCell_SearchWords.Contains(strSearchWords))
                {
                    HtmlTableCell tblCell = tblrow.Cells[0];
                    IList<HtmlInputHidden> iListControls = tblCell.Find.AllByAttributes<HtmlInputHidden>();
                    HtmlInputHidden hiddenID = iListControls[0];
                    return hiddenID.Value;
                }
            }
            return "";
        }

        public static string GetStatus(string strSearchWords)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                SM2Utilities.CommonLoader.load("advanced_search_keywords.aspx");
            }
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlSearchSetup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlSearchSetup");
            HtmlDiv cphPageContent_upKeywordsList = cphPageContent_pnlSearchSetup.Find.ById<HtmlDiv>("cphPageContent_upKeywordsList");
            HtmlTable cphPageContent_gvKeywords = cphPageContent_upKeywordsList.Find.ById<HtmlTable>("cphPageContent_gvKeywords");
            int intCountRows = cphPageContent_gvKeywords.Rows.Count;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvKeywords.AllRows[i];
                HtmlTableCell tblCell_SearchWords = tblrow.Cells[4];

                string strCell_SearchWords = tblCell_SearchWords.TextContent.Trim();
                if (strCell_SearchWords.Contains(strSearchWords))
                {
                    HtmlTableCell tblCell = tblrow.Cells[3];
                    IList<HtmlImage> iListControls = tblCell.Find.AllByAttributes<HtmlImage>();
                    HtmlImage image = iListControls[0];
                    return image.Alt;
                }
            }
            return "";
        }

        public static string GetCategories(string strSearchWords)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                SM2Utilities.CommonLoader.load("advanced_search_keywords.aspx");
            }
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlSearchSetup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlSearchSetup");
            HtmlDiv cphPageContent_upKeywordsList = cphPageContent_pnlSearchSetup.Find.ById<HtmlDiv>("cphPageContent_upKeywordsList");
            HtmlTable cphPageContent_gvKeywords = cphPageContent_upKeywordsList.Find.ById<HtmlTable>("cphPageContent_gvKeywords");
            int intCountRows = cphPageContent_gvKeywords.Rows.Count;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvKeywords.AllRows[i];
                HtmlTableCell tblCell_SearchWords = tblrow.Cells[4];

                string strCell_SearchWords = tblCell_SearchWords.TextContent.Trim();
                if (strCell_SearchWords.Contains(strSearchWords))
                {
                    HtmlTableCell tblCell = tblrow.Cells[5];
                    return tblCell.TextContent.Trim();
                }
            }
            return "";
        }

        public static string GetType(string strSearchWords)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                SM2Utilities.CommonLoader.load("advanced_search_keywords.aspx");
            }
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlSearchSetup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlSearchSetup");
            HtmlDiv cphPageContent_upKeywordsList = cphPageContent_pnlSearchSetup.Find.ById<HtmlDiv>("cphPageContent_upKeywordsList");
            HtmlTable cphPageContent_gvKeywords = cphPageContent_upKeywordsList.Find.ById<HtmlTable>("cphPageContent_gvKeywords");
            int intCountRows = cphPageContent_gvKeywords.Rows.Count;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvKeywords.AllRows[i];
                HtmlTableCell tblCell_SearchWords = tblrow.Cells[4];

                string strCell_SearchWords = tblCell_SearchWords.TextContent.Trim();
                if (strCell_SearchWords.Contains(strSearchWords))
                {
                    HtmlTableCell tblCell = tblrow.Cells[6];
                    return tblCell.TextContent.Trim();
                }
            }
            return "";
        }

        public static string GetParameters(string strSearchWords)
        {
            /*  Load home.aspx is the Profiles Page
             *  Load profile to be used for adding new search
             *  Load Search-Setup page which is advanced_search_keywords.aspx
             */
            if (!TableOfSearchActions.is_advanced_search_keywords_loaded())
            {
                SM2Utilities.CommonLoader.load("advanced_search_keywords.aspx");
            }
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlSearchSetup = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlSearchSetup");
            HtmlDiv cphPageContent_upKeywordsList = cphPageContent_pnlSearchSetup.Find.ById<HtmlDiv>("cphPageContent_upKeywordsList");
            HtmlTable cphPageContent_gvKeywords = cphPageContent_upKeywordsList.Find.ById<HtmlTable>("cphPageContent_gvKeywords");
            int intCountRows = cphPageContent_gvKeywords.Rows.Count;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvKeywords.AllRows[i];
                HtmlTableCell tblCell_SearchWords = tblrow.Cells[4];

                string strCell_SearchWords = tblCell_SearchWords.TextContent.Trim();
                if (strCell_SearchWords.Contains(strSearchWords))
                {
                    HtmlTableCell tblCell = tblrow.Cells[7];
                    return tblCell.TextContent.Trim();
                }
            }
            return "";
        }
    }
}
